<?php

namespace addons\hrecharge\controller\api;

use addons\epay\library\Service;
use addons\hrecharge\model\Hrecharge;
use addons\hrecharge\model\HrechargeSpecs;
use app\admin\model\Recharge;
use app\common\controller\Api;
use think\Db;
use Yansongda\Pay\Pay;

class Index extends Api
{
	protected $noNeedLogin = ['notify','configs'];
	protected $noNeedRight = ['*'];

	public function _initialize()
    {
        parent::_initialize();
    }

	public function index()
    {
        die('111');
    }

    /**
     * 充值配置
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function configs(){
        $config = get_addon_config('hrecharge');
        $config['specs'] = array_values($config['specs']);
        $this->success('',$config);
    }

    /**
     * 余额充值
     */
    public function recharge(){
        $user_id = $this->auth->id;
        $money = $this->request->param('money');
        $way = $this->request->param('way');
        if(empty($money) || !in_array($way,[1,2]))$this->error('参数错误');
        $model = new Hrecharge();
        $ret = $model->add($user_id,$money,$way);
        if($ret)$this->success('',$ret);
        $this->error($model->getError());
    }

    public function notify(){
        $way = $this->request->param('way');
        Db::startTrans();
        try{
            $pay = Pay::$way(Service::getConfig($way));
            $data = $pay->verify(); // 是的，验签就这么简单！
            $data = $data->toArray();
//            Db::name('notify')->insert(['content'=>json_encode($data),'create_time'=>date('Y-m-d H:i:s',time())]);
            if($way=='wechat'){
                if($data['result_code']=='SUCCESS'){
                    $ret = (new Hrecharge())->notify($data['out_trade_no']);
                    if(!$ret){
                        $error = (new Hrecharge())->getError();
                        $this->error();
                    }
                }else{
                    $error = '支付失败2';
                    $this->error();
                }
            }elseif ($way=='alipay'){
                if($data['trade_status']=='TRADE_SUCCESS' || $data['trade_status']=='TRADE_FINISHED'){
                    //TO DO
                    $ret = (new Hrecharge())->notify($data['out_trade_no']);
                    if(!$ret){
                        $error = (new Hrecharge())->getError();
                        $this->error();
                    }
                }else{
                    $error = '支付失败2';
                    $this->error();
                }
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            if(empty($error)){
                $error = $e->getMessage();
            }
            cache('error',$error);
//            Db::name('notify')->insert(['content'=>$error,'create_time'=>date('Y-m-d H:i:s',time())]);
            $this->error($error);
        }
        return $pay->success()->send();// laravel 框架中请直接 `return $pay->success()`
    }

    public function lists(){
        $limit = $this->request->param('limit');
        $model = new Hrecharge();
        $where['user_id'] = $this->auth->id;
        $ret = $model->where($where)->paginate($limit);
        $this->success('',$ret);
    }
}


